import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)
// 引入cookies
import Cookie from "js-cookie";

import {
	resolve
} from 'path'


const router = new Router({
	mode: 'history',
	routes: [{
			path: '/',
			meta: {
				title: 'Crud'
			},
			component: resolve => require(['@/components/login'], resolve),
		}, {
			path: '/regist',
			meta: {
				title: 'Crud'
			},
			component: resolve => require(['@/components/regist'], resolve),
		},
		{
			path: '/manage/',
			meta: {
				requireAuth: true,
				title: 'Crud'
			},
			component: resolve => require(['@/components/manage/index'], resolve),
			children: [{
					path: '/manage/',
					meta: {
						title: '首页',
						requireAuth: true,
					},
					component: resolve => require(['@/components/manage/home'], resolve),
				}, {
					path: '/manage/user/new',
					meta: {
						title: '新员审核',
						requireAuth: true,
					},
					component: resolve => require(['@/components/manage/user/new'], resolve),
				}, {
					path: '/manage/user/lists',
					meta: {
						title: '管理社员',
						requireAuth: true,
					},
					component: resolve => require(['@/components/manage/user/lists'], resolve),
				}, {
					path: '/manage/user/out',
					meta: {
						title: '淘汰社员',
						requireAuth: true,
					},
					component: resolve => require(['@/components/manage/user/out'], resolve),
				},

				{
					path: '/manage/notice/lists',
					meta: {
						title: '公告管理',
						requireAuth: true,
					},
					component: resolve => require(['@/components/manage/notice/lists'], resolve),
				}
			]
		}
	]
})

const originalPush = Router.prototype.push;
Router.prototype.push = function push(location) {
	return originalPush.call(this, location).catch(err => err)
}

// 导航守卫
router.beforeEach((to, from, next) => {
	if (to.meta.requireAuth) { // 判断该路由是否需要登录权限
		if (Cookie.get("number") == undefined) {
			next({
				path: '/' //跳转到登录界面
			})
		} else {
			next()
		}
	} else {
		next()
	}
})

export default router
